package com.example.fx.fg

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.mvvm.BaseFragment
import com.example.fx.R
import com.example.fx.adapter.GoodsSearchAdapter
import com.example.fx.adapter.MenuAdapter
import com.example.fx.adapter.MoneyAdapter
import com.example.fx.databinding.FragmentIndexBinding
import com.example.fx.fgViewModel.IndexViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.loader.ImageLoader

class IndexFragment : BaseFragment<IndexViewModel,FragmentIndexBinding>() {
    val imgs = mutableListOf<String>()//存放图片的集合
    lateinit var menuAdapter: MenuAdapter
    lateinit var moneyAdapter: MoneyAdapter
    lateinit var goodsSearchAdapter: GoodsSearchAdapter
    var type = 4//默认显示第一个，分类的type为4
    var page = 1//当前页面默认为1
    val limit = 5//每页默认显示条数 （总数量为7-8，为了体现分页）
    override fun getLayout(): Int  = R.layout.fragment_index

    override fun getViewModel(): Class<IndexViewModel>  = IndexViewModel::class.java

    override fun initView() {
        //初始化菜单
        menuAdapter = MenuAdapter()
        binding.rvMenu.adapter = menuAdapter
        binding.rvMenu.layoutManager = GridLayoutManager(context,5)
        //初始化省钱菜单
        moneyAdapter = MoneyAdapter()
        binding.rvCate.adapter = moneyAdapter
        binding.rvCate.layoutManager = GridLayoutManager(context,4)
        //执行请求首页数据操作
        viewModel.getIndex()
        //点击跳转到搜索界面
        binding.tvSearch.setOnClickListener {
            ARouter.getInstance().build("/app/SearchActivity").navigation()
        }
        //初始化商品
        goodsSearchAdapter = GoodsSearchAdapter()
        binding.rvGoods.adapter = goodsSearchAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)
        //执行请求商品数据
        viewModel.goodsIndex(type, page, limit)
        //实现加载更多
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                //刷新
                page = 1
                viewModel.goodsIndex(type, page, limit)
                //刷新完成后结束刷新动作
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //加载更多
                page++
                viewModel.goodsIndex(type, page, limit)
                //加载完成后结束加载动作
                binding.srl.finishLoadMore()
            }
        })
        //点击省钱菜单，商品数据发生变化
        moneyAdapter.setOnItemClickListener { adapter, view, position ->
            type = moneyAdapter.data[position].type.toInt()//获取当前点击的菜单的type值，转成Int类型
            moneyAdapter.setType(type)
            page=1//页码成为第一页
            viewModel.goodsIndex(type, page, limit)
        }
        //点击商品列表item,跳转到详情页面
        goodsSearchAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodsSearchAdapter.data[position].id).navigation()
        }

    }

    override fun initData() {
        //观察请求首页数据的结果
        viewModel.indexSuccess.observe(this){
            if(it.code==200){
                //展示页面数据
                //轮播图
                it.data.banner.forEach {
                    imgs.add(it.pic)
                }
                binding.banner.setImages(imgs)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null&&imageView!=null) {
                                Glide.with(context).load(path).into(imageView)
                            }
                        }
                    })
                    .start()
                //展示菜单
                menuAdapter.data.clear()
                menuAdapter.data.addAll(it.data.menus)
                menuAdapter.notifyDataSetChanged()
                //展示省钱菜单
                moneyAdapter.data.clear()
                moneyAdapter.data.addAll(it.data.explosiveMoney)
                moneyAdapter.notifyDataSetChanged()

            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.indexFail.observe(this){
            ToastUtils.showLong(it)
        }
        //观察请求商品结果
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodsSearchAdapter.data.clear()
                }
                goodsSearchAdapter.data.addAll(it.data.list)
                goodsSearchAdapter.notifyDataSetChanged()
                //如果当前未加载到数据，给出提示
                if(it.data.list!=null&&it.data.list.size==0){
                    ToastUtils.showLong("未加载到更多数据")
                }
            }else{
                ToastUtils.showLong(it.message)
            }
        }
    }

}